# -*- coding: utf-8 -*-
'''
@Software: PyCharm
@File    : api_test.py
@Author  : Bryan SHEN
@E-mail  : m18801919240_3@163.com
@Site    : Shanghai, China
@Time    : 2021-09-08
@Description:
'''

import requests
import json
import pandas as pd


url = "http://192.168.3.100:5151/barrage_subject"


def match(comment):

    data = {"items": [comment]}
    # print("comment: ", comment)
    data_json = json.dumps(data)
    res = requests.post(url, data=data_json)
    # print("res: ", res.text)
    line = {}
    # try:
    line = json.loads(res.text)
    # except Exception as e:
    #     print("comment: ", comment)
    #     print("error：", e)

    return line["feedback"]


if __name__ == '__main__':

    # data = pd.read_excel("../data/test.xlsx")
    data = pd.read_excel("../data/clean_data.xlsx")
    items = data.to_dict(orient='records')
    # print(match("抢到了"))

    for i, item in enumerate(items):
        if i % 1000 == 0: print(i)
        feedback = match(item["content"])
        item["emotion"] = feedback[0]["emotion"]
        item["subject"] = feedback[0]["subject"]
        item["point"] = feedback[0]["point"]

    print("items: ", items)

    # output_path = "../data/test_result_0908.xlsx"
    output_path = "../data/clean_data_result_0908_v3.xlsx"

    columns = ["nickname", "content", "emotion", "subject", "point"]

    new_data = pd.DataFrame(items)

    with pd.ExcelWriter(output_path, engine='xlsxwriter', options={'strings_to_urls': False}) as writer:
        new_data.to_excel(writer, sheet_name='sheet1', index=False, columns=columns)




